package com.vr.xuecheng.orders.config;

import com.vr.xuecheng.orders.constant.RabbitMqConstants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author hzh
 * @date 2025/04/10
 * rabbitmq配置
 */
@Configuration
public class RabbitMqConfig{

    /**
     * 支付通知交换机
     */
    @Bean("pay.notify.direct")
    public DirectExchange payNotifyDirectExchange() {
        return new DirectExchange(RabbitMqConstants.PAY_NOTIFY_EXCHANGE, true, false);
    }

    /**
     * 接受支付结果队列
     */
    @Bean("pay.notify.queue")
    public Queue payNotifyQueue() {
        return QueueBuilder.durable(RabbitMqConstants.PAY_NOTIFY_QUEUE)
                .lazy()
                .build();
    }

    /**
     * 交换机和支付队列绑定
     */
    @Bean
    public Binding payNotifyBinding() {
        return BindingBuilder
                .bind(payNotifyQueue())
                .to(payNotifyDirectExchange())
                // routingKey
                .with(RabbitMqConstants.PAY_NOTIFY_ROUTING_KEY);
    }

    /**
     * JSON消息转换器（替代默认的SimpleMessageConverter）
     */
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
